# generate_synthetic_data.py
import pandas as pd
import numpy as np
import random

def generate_normal_transaction():
    """生成正常事务"""
    return {
        "duration_sec": np.random.gamma(shape=2, scale=2),  # 通常 1~20秒
        "wait_time_ms": np.random.exponential(10),          # 等待时间短
        "rows_affected": np.random.poisson(100),           # 影响行数中等
        "lock_count": random.choice([1, 2]),               # 锁少
        "is_long_running": 0
    }

def generate_long_running_transaction():
    """生成长事务（异常）"""
    return {
        "duration_sec": np.random.gamma(shape=5, scale=100), # 300~1000秒
        "wait_time_ms": np.random.uniform(500, 2000),        # 高等待
        "rows_affected": np.random.poisson(10),              # 可能影响少但锁多
        "lock_count": np.random.randint(5, 10),              # 多锁
        "is_long_running": 1
    }

# 生成 1000 条数据：900 正常 + 100 异常
data = []
for _ in range(900):
    data.append(generate_normal_transaction())

for _ in range(100):
    data.append(generate_long_running_transaction())

# 转为 DataFrame
df = pd.DataFrame(data)
df = df[(df['duration_sec'] > 0) & (df['wait_time_ms'] > 0)]  # 过滤异常值
df.to_csv("synthetic_transaction_logs.csv", index=False)

print("✅ 已生成 1000 条合成数据（900 正常 + 100 异常）")
print(df.describe())